package com.aternity.ipresolve;

import com.google.common.base.Stopwatch;

import java.util.Random;

/**
 * @author Daniel Orr
 */
public class ComparePerf {
    public static void main(String[] args) {
        Stopwatch stopwatch1 = Stopwatch.createUnstarted();
        Stopwatch stopwatch2 = Stopwatch.createUnstarted();
        ver2();
//        ver2();
//        ver1();
//        ver2();
    }


    private static void ver1() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        double n = Math.pow(10, 7);
        System.out.println("n="+n);
        for (int i = 0; i < n; i++) {
            String randomXForwardedForHeader = getRandomXForwardedForHeader();
            IpUtils.getIpFromRequest(randomXForwardedForHeader);
        }
        System.out.println(stopwatch);
    }

    private static void ver2() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        double n = Math.pow(10, 7);
        System.out.println("n="+n);
        for (int i = 0; i < n; i++) {
            String randomXForwardedForHeader = getRandomXForwardedForHeader();
            InetAddressUtil.findNonPrivateIpAddress(randomXForwardedForHeader);
        }
        System.out.println(stopwatch);
    }


    static Random r = new Random();

    public static String getRandomXForwardedForHeader(){
        int k = r.nextInt(5);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < k; i++) {
            if(i > 0){
                sb.append(", ");
            }
            if(i%2 == 0){
                sb.append(getRandomPrivateIpAddress());
            }
            else {
                sb.append(getRandomIpAddress());
            }
        }
        return sb.toString();
    }

    public static String getRandomIpAddress(){
        return r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
    }

    public static String getRandomPrivateIpAddress(){
        return "10." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
    }
}
